Outcome

sealed class Outcome<out E, out A>(source)

Outcome is a type that represents three possible states a result can be in: Present, Absent or Failure. Under the hood it wraps the type Either<E, Option<A>> and supports the common functions that Eithers and Options support such as map, flatMap and zip.

There are three primary constructors:

data class Present<A>(val value: A) : Outcome<Nothing, A>
data class Failure<E>(val error: E) : Outcome<E, Nothing>
object Absent : Outcome<Nothing, Nothing>

or you can use the extension methods thusly:

A.present()
E.failure()

You can also easily convert an Either<Option<A>> to an Outcome using toOutcome()

val outcome = "hi".some().right().toOutcome()

There is also a type alias OutcomeOf<A> which specialises the error side to a Throwable for your convenience.

Inheritors

Types

Link copied to clipboard
object Companion

Properties

Link copied to clipboard
val inner: Either<E, Option<A>>

Functions

Link copied to clipboard
inline fun <E, A> Outcome<E, A>.asEither(onAbsent: () -> E): Either<E, A>
Link copied to clipboard
fun <E, A> Outcome<E, A>.asOption(): Option<A>

Converts an Outcome to an option treating Failure as Absent

Link copied to clipboard
inline fun <A, E> Outcome<E, A>.filter(p: (A) -> Boolean): Outcome<E, A>
Link copied to clipboard
inline fun <A, E, B> Outcome<E, A>.flatMap(f: (A) -> Outcome<E, B>): Outcome<E, B>

FlatMap allows multiple Outcomes to be safely chained together, passing the value from the previous as input into the next function that produces an Outcome

Link copied to clipboard
inline fun <A, E, B> Outcome<E, A>.flatTap(f: (A) -> Outcome<E, B>): Outcome<E, A>

Performs a flatMap across the supplied function, propagating failures or absence but preserving the original present value.

Link copied to clipboard
fun <A, E> Outcome<E, Outcome<E, A>>.flatten(): Outcome<E, A>
Link copied to clipboard
inline fun <E, A, B> Outcome<E, A>.fold(onFailure: (E) -> B, onAbsent: () -> B, onPresent: (A) -> B): B
Link copied to clipboard
inline fun <E, A, B> Outcome<E, A>.foldOption(onAbsent: () -> B, onPresent: (A) -> B): Either<E, B>
Link copied to clipboard
inline fun <E, A> Outcome<E, A>.getOrElse(onAbsentOrFailure: () -> A): A
Link copied to clipboard
Link copied to clipboard
Link copied to clipboard
Link copied to clipboard
inline fun <B> map(f: (A) -> B): Outcome<E, B>

Map safely transforms a value in the Outcome. It has no effect on Absent or Failure instances.

Link copied to clipboard
inline fun <E, A, EE> Outcome<E, A>.mapFailure(f: (E) -> EE): Outcome<EE, A>
Link copied to clipboard
inline fun <E, A> Outcome<E, A>.onAbsentHandle(onAbsent: () -> Outcome<E, A>): Outcome<E, A>
Link copied to clipboard
inline fun <E, A> Outcome<E, A>.onFailureHandle(onFailure: (E) -> Outcome<E, A>): Outcome<E, A>
Link copied to clipboard
fun <A> Outcome<Throwable, A>.optionOrThrow(): Option<A>
Link copied to clipboard
inline fun <A> Outcome<Throwable, A>.orThrow(onAbsent: () -> Throwable): A
Link copied to clipboard
inline fun <E, A, EE> Outcome<E, A>.recover(block: OutcomeRaise<EE>.(E) -> A): Outcome<EE, A>
Link copied to clipboard
fun <E, EE, A> Outcome<E, Either<EE, A>>.sequence(): Either<EE, Outcome<E, A>>
fun <E, A> Outcome<E, Option<A>>.sequence(): Option<Outcome<E, A>>
fun <E, EE, A> Outcome<E, Validated<EE, A>>.sequence(): Validated<EE, Outcome<E, A>>
Link copied to clipboard
inline fun <B> tap(f: (A) -> B): Outcome<E, A>

Performs an effect over the value and preserves the original Outcome

Link copied to clipboard
inline fun <A, B, E> Outcome<E, A>.tapAbsent(f: () -> B): Outcome<E, A>
Link copied to clipboard
inline fun <A, B, E> Outcome<E, A>.tapFailure(f: (E) -> B): Outcome<E, A>
Link copied to clipboard
inline fun <E, EE, A, B> Outcome<E, A>.traverse(f: (A) -> Either<EE, B>): Either<EE, Outcome<E, B>>
inline fun <E, A, B> Outcome<E, A>.traverse(f: (A) -> Option<B>): Option<Outcome<E, B>>
inline fun <E, EE, A, B> Outcome<E, A>.traverse(f: (A) -> Validated<EE, B>): Validated<EE, Outcome<E, B>>
inline fun <E, A, B> Outcome<E, A>.traverse(f: (A) -> List<B>): List<Outcome<E, B>>
Link copied to clipboard
inline fun <E, A, B, C> Outcome<E, A>.zip(other: Outcome<E, B>, f: (A, B) -> C): Outcome<E, C>

Zip allows you to combine two or more Outcomes easily with a supplied function.

inline fun <E, A, B, C, D> Outcome<E, A>.zip(o1: Outcome<E, B>, o2: Outcome<E, C>, crossinline f: (A, B, C) -> D): Outcome<E, D>
inline fun <E, A, B, C, D, EE> Outcome<E, A>.zip(o1: Outcome<E, B>, o2: Outcome<E, C>, o3: Outcome<E, D>, crossinline f: (A, B, C, D) -> EE): Outcome<E, EE>
inline fun <E, A, B, C, D, EE, F> Outcome<E, A>.zip(o1: Outcome<E, B>, o2: Outcome<E, C>, o3: Outcome<E, D>, o4: Outcome<E, EE>, crossinline f: (A, B, C, D, EE) -> F): Outcome<E, F>